package com;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;




@WebServlet("/tree")
public class tree extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("--------doget--------");  
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		//level等 属性初始化时就有
        String s1 = "{id:1, pId:0, name:\"根节点\" , open:true ,url2:\"dtIndex.html\"}";  
        String s2 = "{id:2, pId:1, name:\"test2\" , open:false , url2:\"http:www.baidu.com\"}";  
        String s3 = "{id:3, pId:1, name:\"test3\" , open:true , url2:\"http:www.baidu.com\"}";  
        String s4 = "{id:4, pId:2, name:\"test4\" , open:true , url2:\"http:www.baidu.com\"}"; 
        JSONObject obj1 = JSONObject.parseObject(s1);
        JSONObject obj2 = JSONObject.parseObject(s2);
        JSONObject obj3 = JSONObject.parseObject(s3);
        JSONObject obj4 = JSONObject.parseObject(s4);
        
        List<String> lstTree = new ArrayList<String>();  
        lstTree.add(s1);  
        lstTree.add(s2);  
        lstTree.add(s3);  
        lstTree.add(s4);  
        
        JSONArray jsonArr  = new JSONArray();
        jsonArr.add(obj1);
        jsonArr.add(obj2);
        jsonArr.add(obj3);
        jsonArr.add(obj4);
        
        System.out.println(jsonArr.toString());
        //利用Json插件将Array转换成Json格式  
        response.getWriter().print(jsonArr); 
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("---初始话------------");
		doGet(request,response);
	}

}
